package meijuli.com.join51.business.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.Options;
import java.util.List;

/**
 * 首页菜单适配器
 *
 * @author wenqin 2017-04-25 15:57
 */

public class OptionsAdapter extends RecyclerView.Adapter<OptionsAdapter.OpVH> {

    private LayoutInflater mInflater;
    private List<Options> mOptionsList;
    private OnItemClickListener mOnItemClickListener;

    public OptionsAdapter(Context context, List<Options> optionsList) {
        mOptionsList = optionsList;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public OpVH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OpVH(mInflater.inflate(R.layout.include_entrance, parent, false));
    }

    @Override
    public void onBindViewHolder(OpVH holder, int position) {
        holder.mIcon.setImageResource(mOptionsList.get(position).getIcon());
        holder.mTitle.setText(mOptionsList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return mOptionsList == null ? 0 : mOptionsList.size();
    }

    class OpVH extends RecyclerView.ViewHolder {
        ImageView mIcon;
        TextView mTitle;

        public OpVH(View itemView) {
            super(itemView);
            mIcon = itemView.findViewById(R.id.icon);
            mTitle = itemView.findViewById(R.id.title);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.itemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void itemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
